<?php
include_once("../configure/admin.config.inc.php");	//--> admin global var
include_once("db.inc.php");							//--> db global var
include_once("Smarty.class.php");					//--> out template
include_once("Operation.php");						//--> Operation
include_once("Special.php");						//--> Special
include_once("Product.php");						//--> Product
require_once("controlHeader.php");					//--> system control header

$objOperate 	= new Operation($objSession->getLanguage());		//--> Operation
$objSpecial		= new Special(DB_TAG_SYSTEM, $uid);					//--> Special
$oProduct 		= new Product(DB_TAG_SYSTEM, $uid);					//--> Product

$error_message 	= '';

$productID		= '';
$expireDate		= '';
$newPrice 		= '';

$arrOperate = $objOperate->arrGetFromGroupIDAndMenuID($gid, $menuid);
if(!empty($id)){
	$arrData 		= $objSpecial->getByID($arrOperate, $id);
	
	$productID		= $arrData['productID'];
	$expireDate		= substr($arrData['expireDate'], 0, 10);
	$newPrice 		= $arrData['newPrice'] / 100;
}

if(isset($_POST['Submit'])){
	$id 		= $_POST['id'];
	$productID	= $_POST['productID'];
	$expireDate	= $_POST['expireDate'];
	$newPrice 	= $_POST['newPrice'];
 	
 	if(empty($expireDate))
 		$expireDate = '0000-00-00 00:00:00';
 	else
 		$expireDate = $_POST['expireDate'];
 	
	if(empty($productID)){
		$error_message = 'The product ID should\'t be empty.';
	}else if(empty($newPrice)){
		$error_message = 'The new price should\'t be empty.';
	}
	
	if(empty($error_message)){
		$isReturnOrg = $objSpecial->edit($arrOperate, $id, $productID, $newPrice, $expireDate);
		if($isReturnOrg)
			$error_message = 'edit successfully.';
		else{
			$error_message = 'edit failure1.';
		}
	}
}

/*--------- get product list ---------------------*/
$arrProducts = array();
$where	= ' AND p.status in ("normal", "hot")';
$order	= 'ORDER BY pd.name';

$arrDataList = $oProduct->lists($arrOperate, $LANGEUAGE_ID, $where, $order, 0, 10000, '');
foreach($arrDataList as $arrRow){
	$arrProducts[$arrRow['id']] = $arrRow['name'] . " ($CURRENCY" . ($arrRow['price'] / 100) . ')';
}
 
/*----- out html -----*/
$smarty = new Smarty();		//----- out template
$smarty->template_dir = TEMPLATE_SYS_DIR;
$smarty->compile_dir  = COMPILE_SYS_DIR;
$smarty->assign('menuGid', $menuGid);
$smarty->assign('menuid', $menuid);
$smarty->assign('id', $id);

$smarty->assign('error_message', $error_message);
$smarty->assign('backurl',$backurl);

$smarty->assign('arrProducts', $arrProducts);
$smarty->assign('newPrice', $newPrice);
$smarty->assign('productID', $productID);
$smarty->assign('expireDate', $expireDate);

$smarty->display('listSpecial_Edit.htm');
?>
